package com.hd.trans.config

import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.hd.trans.db.bean.TransContentMode
import com.hd.trans.db.bean.TransFontSize
import com.hd.trans.network.PreferenceMgr
import com.hd.trans.ui.base.AppContextProvider
import com.translation.assistant.data.bean.LanguageMapping

object TransConfigs {
    private val gson: Gson by lazy {
        GsonBuilder().create()
    }
    private val sp: SharedPreferences by lazy {
        AppContextProvider.getContext().getSharedPreferences("TransConfigs", 0)
    }

    private const val KEY_offlineTransFileSizeMap = "offlineTransFileSizeMap"
    private const val KEY_docFromLanguage = "docFromLanguage"
    private const val KEY_docToLanguage = "docToLanguage"
    private const val KEY_isPinyinEnable = "isPinyinEnable"
    private const val KEY_isTextLineBreak = "isTextLineBreak"
    private const val KEY_transFontSize = "transFontSize"
    private const val KEY_subtitleTimeAxisEnable = "subtitleTimeAxisEnable"
    private const val KEY_subtitleFontSize = "subtitleFontSize"
    private const val KEY_subtitleContentMode = "subtitleContentMode"

    /**
     * 离线翻译文件大小
     */
    private val DefOfflineTransFileSizeMap: HashMap<String,Long> = hashMapOf(
        "de" to 29027076L,
        "hi" to 29550580L,
        "pt" to 26754875L,
        "lv" to 28838561L,
        "hr" to 27656885L,
        "hu" to 27205422L,
        "uk" to 28446659L,
        "mg" to 31798932L,
        "id" to 29173536L,
        "ur" to 28139027L,
        "mk" to 25259979L,
        "mr" to 29989541L,
        "ms" to 26144734L,
        "el" to 27109468L,
        "mt" to 28540954L,
        "en" to 26214400L,
        "is" to 25000249L,
        "it" to 26034013L,
        "my" to 30330487L,
        "es" to 27848527L,
        "zh" to 32202824L,
        "et" to 26959473L,
        "ar" to 27080919L,
        "vi" to 28960902L,
        "ja" to 26809855L,
        "fa" to 28128156L,
        "ro" to 27324759L,
        "nl" to 27527032L,
        "no" to 28632829L,
        "fi" to 24393499L,
        "ru" to 29506823L,
        "bg" to 28343117L,
        "fr" to 25388808L,
        "bn" to 28558843L,
        "bs" to 28179384L,
        "ka" to 31010233L,
        "sk" to 27538171L,
        "sl" to 29827094L,
        "ca" to 28126753L,
        "sr" to 26744322L,
        "km" to 27783159L,
        "sv" to 26328685L,
        "ko" to 28280217L,
        "ta" to 28008678L,
        "cs" to 27659161L,
        "pa" to 31758108L,
        "te" to 31348119L,
        "th" to 27697028L,
        "tl" to 28078583L,
        "pl" to 26000631L,
        "da" to 28789573L,
        "he" to 29062994L,
        "tr" to 27686675L,
    )

    /**
     * 离线翻译支持的语言集合
     */
    val SupportOfflineLanguageSet = LinkedHashSet<String>()

    /**
     * 离线翻译文件已下载大小
     */
    val offlineTransFileAlreadySizeMap = HashMap<String, Long>()

    /**
     * 离线翻译文件大小
     */
    var offlineTransFileSizeMap:HashMap<String, Long>
    get() {
        val str = sp.getString(KEY_offlineTransFileSizeMap, "")
        return if (str.isNullOrEmpty()) {
            DefOfflineTransFileSizeMap
        } else {
            gson.fromJson(str,object : TypeToken<HashMap<String, Long>>() {}.type)
        }
    }
    set(value) {
        sp.edit().putString(KEY_offlineTransFileSizeMap, gson.toJson(value)).apply()
    }

    /**
     * 文本翻译源语言
     */
    var textTransFromLangName: String
    get() {
        return PreferenceMgr.getInstance().translatePreference.tansFromLanguage
    }
    set(value) {
        PreferenceMgr.getInstance().translatePreference.saveTansFromLanguage(value)
    }

    /**
     * 文本翻译目标语言
     */
    var textTransToLangName: String
    get() {
        return PreferenceMgr.getInstance().translatePreference.tansToLanguage
    }
    set(value) {
        PreferenceMgr.getInstance().translatePreference.saveTansToLanguage(value)
    }

    /**
     * 拍照翻译源语言
     */
    var fileTransFromLangName: String
    get() {
        return PreferenceMgr.getInstance().translatePreference.fileFromLanguage
    }
    set(value) {
        PreferenceMgr.getInstance().translatePreference.saveFileFromLanguage(value)
    }

    /**
     * 拍照翻译目标语言
     */
    var fileTransToLangName: String
    get() {
        return PreferenceMgr.getInstance().translatePreference.fileToLanguage
    }
    set(value) {
        PreferenceMgr.getInstance().translatePreference.saveFileToLanguage(value)
    }

    /**
     * 文档翻译源语言
     */
    var docTransFromLangName: String
    get() {
        return sp.getString(KEY_docFromLanguage, "英语")!!
    }
    set(value) {
        sp.edit().putString(KEY_docFromLanguage, value).apply()
    }

    /**
     * 文档翻译目标语言
     */
    var docTransToLangName: String
    get() {
        return sp.getString(KEY_docToLanguage, "中文（简体）")!!
    }
    set(value) {
        sp.edit().putString(KEY_docToLanguage, value).apply()
    }

    /**
     * 是否显示拼音
     */
    var isPinyinEnable:Boolean
    get() {
        return sp.getBoolean(KEY_isPinyinEnable, false)
    }
    set(value) {
        sp.edit().putBoolean(KEY_isPinyinEnable, value).apply()
    }

    /**
     * 文本换行
     */
    var isTextLineBreak:Boolean
    get() {
        return sp.getBoolean(KEY_isTextLineBreak, false)
    }
    set(value) {
        sp.edit().putBoolean(KEY_isTextLineBreak, value).apply()
    }

    var fontSize: TransFontSize
    get() {
        val str = sp.getString(KEY_transFontSize, "")
        return if (str.isNullOrEmpty()) {
            TransFontSize.Normal
        } else {
            TransFontSize.valueOf(str)
        }
    }
    set(value) {
        sp.edit().putString(KEY_transFontSize, value.name).apply()
    }

    fun isSupportOfflineLanguage(code:String):Boolean{
        return SupportOfflineLanguageSet.contains(LanguageMapping.getLangOfflineCode(code))
    }

    var subtitleTimeAxisEnable: Boolean
        get() {
            return sp.getBoolean(KEY_subtitleTimeAxisEnable, true)
        }
        set(value) {
            sp.edit().putBoolean(KEY_subtitleTimeAxisEnable, value).apply()
        }

    var subtitleFontSize: TransFontSize
        get() {
            val str = sp.getString(KEY_subtitleFontSize, "")
            return if (str.isNullOrEmpty
                    ()
            ) {
                TransFontSize.Normal
            } else {
                TransFontSize.valueOf(str)
            }
        }
        set(value) {
            sp.edit().putString(KEY_subtitleFontSize, value.name).apply()
        }

    var subtitleContentMode: TransContentMode
        get() {
            val str = sp.getString(KEY_subtitleContentMode, "")
            return if (str.isNullOrEmpty()) {
                TransContentMode.Both
            } else {
                TransContentMode.valueOf(str)
            }
        }
        set(value) {
            sp.edit().putString(KEY_subtitleContentMode, value.name).apply()
        }




}